﻿#ifndef WIDGETPLCSERVER_H
#define WIDGETPLCSERVER_H

#include <QGroupBox>
#include <QHBoxLayout>
#include <QPushButton>
#include <QComboBox>
#include <QSpinBox>
#include <QCheckBox>
#include <QTextEdit>
#include <QLineEdit>
#include <QTableWidget>
#include "PLCServerBase.h"

class WidgetPLCServer : public QGroupBox
{
    Q_OBJECT
public:
    explicit WidgetPLCServer(const QString& title = "", QWidget *parent = nullptr);

    void addPLCServer(PLCServerBase *server);
signals:
    void sigConfigChanged(const PLCServerConfig& config);
    void sigAddOne(const PLCServerConfig& config);
    void sigDeleteOne(const QString& name);
    void sigSendRequest(PLCRequestData data);
private:
    void setupUI();
    void setupConfigUI();
    void setupSendRev();
    void initConnection();
    PLCServerConfig getConfig();
    void onSaveConfig();
    void onComSelectChanged(const int& index);
    double getValue(int value);
    QString getTxt(int index);
private:
    QMap<QString, PLCServerBase*> mMapPLCServer;
    bool mIsRequest = false;
    //control
    QHBoxLayout* mLayout;
    //PLC配置控件
    QPushButton* pbtnAddOne;
    QPushButton* pbtnDeleteOne;
    QPushButton* pbtnApplyPLC;
    QComboBox* cbboxComNameList;
    QComboBox* cbboxComTypeList;
    QSpinBox* spboxServerID;

    //串口
    QWidget* wgtSerialConfig;
    QComboBox* cbboxSerialList;
    QComboBox* cbboxSerialBaudRate;
    QComboBox* cbboxSerialDataBits;
    QComboBox* cbboxSerialParity;
    QComboBox* cbboxSerialStopBits;
    QComboBox* cbboxSerialFlowControl;

    //socket
    QWidget* wgtSocketConfig;
    QLineEdit* ledtOpponentIP;
    QSpinBox* spboxOpponentPort;
    QLineEdit* ledtLocalIP;
    QSpinBox* spboxLocalPort;

    //接收区
    QCheckBox* chkboxIsDisplayData;
    QPushButton* pbtnClear;
    QTextEdit* txtedtDataRev;

    //发送区
    QSpinBox* spboxAddrStart;
    QComboBox* cbboxDataType;
    QTableWidget* tblwgtReadList;
    QPushButton* pbtnDataAdd;
    QPushButton* pbtnDataRemove;
    QPushButton* pbtnDataRead;
    QPushButton* pbtnDataWrite;
};

#endif // WIDGETPLCSERVER_H
